package com.welike.shibo.utils;

import com.welike.shibo.eneity.JsonResult;
import com.welike.shibo.enums.ResultEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * 请求结果处理类
 * @author fangyu
 * @version v1.0.0
 * @since 2019-08-19 00:24
 */
public class ResultUtils {

    public static JsonResult result(Integer code, String message, Map<String, Object> map) {
        JsonResult result = new JsonResult();
        result.setCode(code);
        switch (code) {
            case 0:
                result.setMessage("成功");
                if (map != null) {
                    result.setData(map);
                }
                break;
            case 1001:
                result.setMessage("token无效, 请重新登录!");
                break;
            case 1002:
                result.setMessage("token过期, 请重新登录!");
            case 1003:
                result.setMessage("请求传参错误!");
            case 404:
                if (Tools.isNull(message)){
                    message = "失败";
                }
                result.setMessage(message);
                break;
            default:
                result.setMessage("未知错误");
                break;
        }
        return result;
    }

    /**
     *
     * @return 0 普通成功信息，无任何data
     */
    public static JsonResult success(){
        Map<String, Object> map = new HashMap<>();
        return new JsonResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), map);
    }

    /**
     * 返回只有成功信息的 ResultJson
     * @param message 返回信息
     * @return 0 普通成功信息，无任何data
     */
    public static JsonResult success(String message){
        Map<String, Object> map = new HashMap<>(1);
        return new JsonResult(ResultEnum.SUCCESS.getCode(), message, map);
    }
    /**
     * 返回带多个key的data ResultJson
     * @param map 带data的成功信息
     * @return
     */
    public static JsonResult success(Map<String, Object> map) {
        return new JsonResult(ResultEnum.SUCCESS, map);
    }

    /**
     * 返回只有一个key的data ResultJson
     * @param key 返回参数的key
     * @param object 带data的成功信息对象
     * @return
     */
    public static JsonResult success(String key, Object object) {
        Map<String, Object> result = new HashMap<>();
        result.put(key, object);
        return new JsonResult(ResultEnum.SUCCESS, result);
    }

    /**
     * 返回带信息和带多个key的data ResultJson
     * @param message
     * @param map
     * @return
     */
    public static JsonResult success(String message, Map<String, Object> map) {
        JsonResult result = new JsonResult();
        result.setData(map);
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMessage(message);
        return result;
    }

    /**
     * 返回带500的错误信息 ResultJson
     * @param message 错误信息
     * @return
     */
    public static JsonResult error(String message){
        Map<String, Object> map = new HashMap<>();
        JsonResult result = new JsonResult();
        result.setCode(500);
        result.setMessage(message);
        result.setData(map);
        return result;
    }

    /**
     * 返回ResultEnum 的错误信息 ResultJson
     * @param resultEnum
     * @return
     */
    public static JsonResult error(ResultEnum resultEnum){
        JsonResult result = new JsonResult();
        result.setCode(resultEnum.getCode());
        result.setMessage(resultEnum.getMsg());
        return result;
    }

    /**
     * 返回自定义错误信息的 ResultJson
     * @param code
     * @param message
     * @return
     */
    public static JsonResult error(Integer code, String message){
        Map<String, Object> map = new HashMap<>(1);
        JsonResult result = new JsonResult();
        result.setCode(code);
        result.setMessage(message);
        result.setData(map);
        return result;
    }

    public static JsonResult error(Integer code, String message, Map<String, Object> map){
        JsonResult result = new JsonResult();
        result.setCode(code);
        result.setMessage(message);
        result.setData(map);
        return result;
    }
}
